package cn.edu.zzuli.message.news.service;

import cn.edu.zzuli.common.utils.NowDate;
import cn.edu.zzuli.common.utils.WeChatUtil;
import cn.edu.zzuli.message.news.mapper.OfferMessageMapper;
import cn.edu.zzuli.message.news.mapper.UserMessageMapper;
import cn.edu.zzuli.model.dto.WeMessage;
import cn.edu.zzuli.model.entity.OfferOrder;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.model.vo.wx.WxMessage;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class SendServiceImpl{


    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private OfferMessageMapper offerMessageMapper;


    public void sendChatMessage(int toUid) {
        String openId = this.getOpenId(toUid);
        User user = userMessageMapper.selectById(toUid);
        JSONObject json = new JSONObject();
        json.set("time1", new JSONObject().set("value", NowDate.getNowTime()));
        json.set("thing2", new JSONObject().set("value", "收到用户" + user.getNickName() + "的消息"));
        try {
            WeChatUtil.wxSend(WxMessage.USER_CHAT,openId,json);
        } catch (Exception e) {
            log.info("---------->!!{}!!<---------","取消订单发送异常");
        }
    }


    public void sendCancelMessage(int toUid) {
        String openId = this.getOpenId(toUid);
        JSONObject json = new JSONObject();
        json.set("character_string4", new JSONObject().set("value", "z-order-" + toUid));
        json.set("time1", new JSONObject().set("value", NowDate.getNowTime()));
        json.set("phrase3", new JSONObject().set("value", "已取消"));
        try {
            WeChatUtil.wxSend(WxMessage.CANCEL_ORDER,openId,json);
        } catch (Exception e) {
            log.info("---------->!!{}!!<---------","取消订单发送异常");
        }
    }

    public void sendApplyMessage(int id, int uid) {
        Map<String, String> map = sendConditions(id, uid);
        JSONObject json = new JSONObject();
        json.set("thing2", new JSONObject().set("value", map.get("title")));
        json.set("thing3", new JSONObject().set("value",map.get("nickName")));
        json.set("time4", new JSONObject().set("value", NowDate.getNowTime()));
        try {
            WeChatUtil.wxSend(WxMessage.APPLY_FOR_CONFIRMATION,map.get("openId"),json);
        } catch (Exception e) {
            log.info("---------->!!{}!!<---------","申请确认发送异常");
        }
    }

    public void sendTakeMessage(int id,int uid) {
        Map<String, String> map = sendConditions(id, uid);
        JSONObject json = new JSONObject();
        json.set("thing2", new JSONObject().set("value", map.get("nickName")));
        json.set("time4", new JSONObject().set("value", NowDate.getNowTime()));
        json.set("thing5", new JSONObject().set("value", map.get("title")));
        try {
            WeChatUtil.wxSend(WxMessage.TAKE_ORDER,map.get("openId"),json);
        } catch (Exception e) {
            log.info("---------->!!{}!!<---------","接单通知发送异常");
        }

    }

    //获取发送信息条件
    private Map<String,String> sendConditions(int id,int uid){
        Integer toUid = this.getOfferUidByOid(id);
        System.out.println(toUid);
        String openId = this.getOpenId(toUid);
        WeMessage weMessage = this.getNickNameAndTitle(uid,id);
        Map<String, String> map = new HashMap<>();
        map.put("openId", openId);
        map.put("nickName",weMessage.getNickName());
        map.put("title",weMessage.getTitle());
        return map;
    }


    //获取昵称和标题
    private WeMessage getNickNameAndTitle(int uid, int id) {
        return offerMessageMapper.getNickNameAndTitle(uid,id);
    }

    //获取用户编号
    public Integer getOfferUidByOid(int id) {
        OfferOrder offerOrder = offerMessageMapper.selectOne(new QueryWrapper<OfferOrder>().select("ouid").eq("oid", id));
        return offerOrder.getOuid();
    }

    //获取openid
    private String getOpenId(int toUid) {
        User user = userMessageMapper.selectOne(new QueryWrapper<User>().select("open_id").eq("uid", toUid));
        return user.getOpenId();
    }
}
